// pages/collect/collect.js
const app=getApp()
let arrMusic=[]
let isBtnMusic=true
// let num=0;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        musicWidth:'120rpx',
        musicHeight:'120rpx',
        videoWidth:'120rpx',
        videoHeight:'120rpx',
        jokeWidth:'120rpx',
        jokeHeight:'120rpx',
        // title:'我喜欢的音乐',
        list:arrMusic,
        startX:0,
        startY:0,
        // stopSrc:'https://s3.bmp.ovh/imgs/2021/11/a67c923cd8edbf2a.png',
        // startSrc:'https://s3.bmp.ovh/imgs/2021/11/982f4f3e0af32e4e.png'
        
    },

    // 开始滑动
    touchStart(e){
        console.log(e)
        let dataList=[...this.data.list]
        dataList.forEach(item=>{
            if(item.isTouchMove){
                item.isTouchMove=!item.isTouchMove;
            }
            console.log(item.isTouchMove)
        })
        
        this.setData({
            list:dataList,
            startX:e.touches[0].clientX,
            startY:e.touches[0].clientY
        })
    },
    touchMove(e){
        console.log(e);
        let moveX=e.changedTouches[0].clientX;
        let moveY=e.changedTouches[0].clientY;
        let id=e.currentTarget.dataset.index;
        console.log(moveX,moveY)
        console.log(this.data.startX,this.data.startY)
        let dataList=[...this.data.list]
        let angle=this.angle({
            X:this.data.startX,
            Y:this.data.startY
        },{
            X:moveX,
            Y:moveY
        });
        dataList.forEach((item,index)=>{
            item.isTouchMove=false;
            if(angle>30){
                return 
            }
            if(id==index){
                if(moveX > this.data.startX){
                    item.isTouchMove=false;
                }else{
                    item.isTouchMove=true;
                }
            }
        })
        console.log(dataList)
        this.setData({
            list:dataList
        })
    },

    // 计算滑动角度
    angle:function(start,end){
        var _X=end.X-start.X,
            _Y=end.Y-start.Y
        return 360*Math.atan(_Y/_X)/(2*Math.PI)
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        isBtnMusic=true
    },
    delItem(e){
        let id=e.currentTarget.dataset.id;
        let dataList=[...this.data.list];
        console.log(id)

        for(let i=0;i<dataList.length;i++){
            const item=dataList[i];
            item.isTouchMove=false;
            if(item.id===id){
                dataList.splice(i,1);
                break;
            }
        }
        this.setData({
            list:dataList
        })
        console.log(id)
        wx.request({
            url:'https://wx.xxmtcds.top:2333/user/collectmusic',
            method:'POST',
            dataType:'text',
            header:{
                authorization:app.globalData.token
            },
            data:{
                id:id
            },
            success:res=>{
                console.log(res) 
                wx.showToast({
                  title: '删除成功！'
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.innerAudioContext=wx.createInnerAudioContext();
        this.innerAudioContext.autoplay=true
        this.innerAudioContext.onError(function(res){
            console.log(res);
            wx.showToast({
                title:'音乐播放失败',
                icon:'none'
            })
        })
    },
    btnMusic:function(){
        wx.redirectTo({
          url: '/pages/collectMusic/collectMusic',
        })
        
    },
    btnVideo:function(){
        wx.redirectTo({
          url: '/pages/collectVideo/collectVideo',
        })
        
        // isBtnMusic=false
        this.setData({
            // title:'我喜欢的视频',
            videoWidth:'170rpx',
            videoHeight:'170rpx',
            musicWidth:'120rpx',
            musicHeight:'120rpx',
            jokeWidth:'120rpx',
            jokeHeight:'120rpx'
        })
    },
    btnEmoil:function(){
        wx.redirectTo({
            url: '/pages/collectJoke/collectJoke',
          })
        // isBtnMusic=false
        this.setData({
            jokeWidth:'170rpx',
            jokeHeight:'170rpx',
            musicWidth:'120rpx',
            musicHeight:'120rpx',
            videoWidth:'120rpx',
            videoHeight:'120rpx',
        })
    },
    btnPlay:function(e){
        console.log(e)
        console.log(this.data.list)
        let arr=this.data.list
        for(let i=0;i<this.data.list.length;i++){
            if(e.target.id==this.data.list[i].id){
                this.innerAudioContext.src=this.data.list[i].url;
                if(this.data.list[i].isPlay==false){
                    arr[i].isPlay=true
                    this.setData({
                        list:arr
                    })
                    this.innerAudioContext.play()
                    console.log(i)
                    console.log(this.data.list[i].isPlay)
                }else{ 
                    arr[i].isPlay=false
                    this.setData({
                        list:arr
                    })
                    this.innerAudioContext.pause()
                }
                console.log(this.data.list[i])
                this.innerAudioContext.onEnded(()=>{
                    this.data.list[i].isPlay=false
                    console.log(i)
                    if(i==this.data.list.length-1){
                        let i=0
                        this.innerAudioContext.src=this.data.list[i].url
                        this.data.list[i].isPlay=true
                    }else{
                        i=i+1
                        this.innerAudioContext.src=this.data.list[i].url
                        this.data.list[i].isPlay=true
                    }
                    this.innerAudioContext.play()
                    console.log(this.data.list[i])
                })
            }else{
                arr[i].isPlay=false
                this.setData({
                    list:arr
                })
            }
        } 
        console.log(this.data.list)
    },

    /**
     * 生命周期函数--监听页面显示 
     */
    onShow: function () {

        const that=this
        let style=wx.getStorageSync('style')
        if(style!=''){
            this.setData({
                contentStyle:wx.getStorageSync('style')
            })
        }else{
            this.setData({
                contentStyle:'https://pic.imgdb.cn/item/6199ad962ab3f51d91e21dd0.jpg'
            })
        }
        wx.request({
            url:'https://wx.xxmtcds.top:2333/user/collectmusic',
            header:{
                authorization:app.globalData.token
            },
            success:res=>{
                arrMusic=res.data
                console.log(arrMusic) 
                
                let a=[]
                console.log(arrMusic)
                for(let i=0;i<arrMusic.length;i++){
                    let arr={
                        id:arrMusic[i].music_id,
                        name:arrMusic[i].music_name,
                        singer:arrMusic[i].music_singer,
                        // src:'https://s3.bmp.ovh/imgs/2021/11/a67c923cd8edbf2a.png',
                        url:arrMusic[i].music_url,
                        isTouchMove:false,
                        isPlay:false
                    }
                    a=that.data.list
                    if(isBtnMusic){
                        a.push(arr)
                        console.log(a)
                    }
                    
                }
                isBtnMusic=false
                this.setData({
                    list:a,
                    musicWidth:'170rpx',
                    musicHeight:'170rpx',
                    videoWidth:'120rpx',
                    videoHeight:'120rpx',
                    jokeWidth:'120rpx',
                    jokeHeight:'120rpx'
                })
            }
        })

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide:function(){
        if(this.data.flag==true){
          this.innerAudioContext.stop()
          this.innerAudioContext.destory()
        }
      },
      onUnload:function(){
        if(this.data.flag==true){
          this.innerAudioContext.stop()
          this.innerAudioContext.destory()
        }
      },

    

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})